﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using CNFM_CROCODILE_LOGO.Core.Domain.ViewModels;
using CNFM_CROCODILE_LOGO.RestClient.Domain;
using CNFM_CROCODILE_LOGO.Services.Interfaces;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace CNFM_CROCODILE_LOGO_Web.Controllers
{
    /// <summary>
    /// 企业管理
    /// </summary>
    public class EnterpriseController : Controller
    {
        private IBasicServices _pro;
        public EnterpriseController(IBasicServices pro)
        {
            this._pro = pro;
        }

        /// <summary>
        /// 企业列表
        /// </summary>
        /// <returns></returns>
        public IActionResult List()
        {
            return View();
        }

        /// <summary>
        /// 企业媒体列表
        /// </summary>
        /// <returns></returns>
        public IActionResult MediaList()
        {
            return View();
        }

        /// <summary>
        /// 企业媒体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult MediaEdit(string id)
        {
            ViewData["Id"] = id;
            return View();
        }

        /// <summary>
        /// 企业修改
        /// </summary>
        /// <returns></returns>
        public IActionResult Edit(string id)
        {
            var result = _pro.GetEnterprise(id);
            var json = JsonConvert.SerializeObject(result.Result);
            JObject obj = JObject.Parse(json);
            EnterpriseModels entity = JsonConvert.DeserializeObject<EnterpriseModels>(obj["Data"].ToString());
            ViewData["Id"] = id;
            ViewData["JYUrl"] = entity.JYUrl;
            ViewData["XYUrl"] = entity.XYUrl;
            return View(entity);
        }
        /// <summary>
        /// 企业新增
        /// </summary>
        /// <returns></returns>
        public IActionResult Add()
        {
            return View();
        }

        /// <summary>
        /// 企业列表
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="sasearch"></param>
        /// <returns></returns>
        [Authorize("Permission")]
        public JsonResult GetEnterpriseAll(int index, int size, string areUnCode, string sasearch, string enterpriseId)
        {
            var ret = _pro.GetEnterpriseAll(index, size, areUnCode, sasearch, enterpriseId);
            return Json(ret);
        }

        /// <summary>
        ///  企业新增修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddUpdateEnterprise([FromBody]EnterpriseModels model)
        {
            List<EnterprisesModels> list = JsonConvert.DeserializeObject<List<EnterprisesModels>>(model.Enterprisestring.ToString());
            model.Enterprises = list;
            var ret = _pro.AddUpdateEnterprise(model);
            return Json(ret);
        }

        /// <summary>
        /// 企业删除
        /// </summary>
        /// <param name="enterpriseId"></param>
        /// <returns></returns>
        [HttpDelete]
        public JsonResult DelectEnterprise(string enterpriseId)
        {
            var ret = _pro.DelectEnterprise(enterpriseId);
            return Json(ret);
        }

        /// <summary>
        /// 企业下拉列表
        /// </summary>
        /// <param name="areCode"></param>
        /// <returns></returns>
        public JsonResult GetEnterpriseTree(string areCode)
        {
            var ret = _pro.GetEnterpriseTree(areCode);
            return Json(ret);
        }

        /// <summary>
        /// 企业详情列表
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult GetEnterprisesAll(int index, int size, string id)
        {
            var ret = _pro.GetEnterprisesAll(index, size, id);
            return Json(ret);
        }

        /// <summary>
        /// 企业宣传资料上传
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult GetEnterPrisePropagandaAll(string id)
        {
            var ret = _pro.GetEnterPrisePropagandaAll(id);
            return Json(ret);
        }

        /// <summary>
        /// 企业宣传文件删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public JsonResult DeleteEnterPrisePropaganda(string id)
        {
            var ret = _pro.DeleteEnterPrisePropaganda(id);
            return Json(ret);
        }
    }
}